package com.qf.ts.platform.front.web;

import com.qf.ts.platform.commons.pojo.TableData;
import com.qf.ts.platform.commons.util.JSONUtil;
import com.qf.ts.platform.front.feign.CarSourceSearchServiceClient;
import com.qf.ts.platform.front.pojo.CarSourceSearchPojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author xianchang
 */
@RestController
@RequestMapping("/carSource/search")
@Api(value = "车源信息的综合搜索")
public class SearchCarSourceController {

    @Resource
    private CarSourceSearchServiceClient carSourceSearchServiceClient;

    @GetMapping("/comprehensive")
    @ApiOperation(value = "车源综合搜索", notes = "本接口用来查询符合条件的车源信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询后显示起始页数", required = true, dataType = "Integer", example = "1"),
            @ApiImplicitParam(name = "limit", value = "查询后每页显示条数", required = true, dataType = "Integer", example = "10")
    })
    public TableData compreSearch(CarSourceSearchPojo pojo, int page, int limit) {

        //分页设置
        pojo.setStart((page - 1) * limit);
        pojo.setRows(limit);

        String json = JSONUtil.toJson(pojo);

        //远程调用---统计es数据的数量
        long count = carSourceSearchServiceClient.searchCount(json);
        if (count > 0) {
            //远程调用---进行es日志查询
            List<Map> list = carSourceSearchServiceClient.compreSearch(json);

            //TODO:是否可以考虑,把从es中查询到的日志数据,缓存到redis中?---->key=logs_1/2/3,value=list

            //分页数量需要单独设置
            return new TableData<>(count, list);
        }

        return new TableData<>(0L, null);
    }

    @GetMapping("/deweight")
    @ApiOperation(value = "车源综合去重搜索", notes = "本接口用来查询符合条件的车源信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询后显示起始页数", required = true, dataType = "Integer", example = "1"),
            @ApiImplicitParam(name = "limit", value = "查询后每页显示条数", required = true, dataType = "Integer", example = "10")
    })
    public TableData deweightSearch(CarSourceSearchPojo pojo, int page, int limit) {

        //分页设置
        pojo.setStart((page - 1) * limit);
        pojo.setRows(limit);

        String json = JSONUtil.toJson(pojo);

        //远程调用---统计es数据的数量
        long count = carSourceSearchServiceClient.searchCount(json);
        if (count > 0) {
            //远程调用---进行es日志查询
            List<Map> list = carSourceSearchServiceClient.deweightSearch(json);

            //TODO:是否可以考虑,把从es中查询到的日志数据,缓存到redis中?---->key=logs_1/2/3,value=list

            //分页数量需要单独设置
            return new TableData<>(count, list);
        }

        return new TableData<>(0L, null);
    }

    @GetMapping("/hight")
    @ApiOperation(value = "车源综合高级搜索", notes = "本接口用来查询符合条件的车源信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询后显示起始页数", required = true, dataType = "Integer", example = "1"),
            @ApiImplicitParam(name = "limit", value = "查询后每页显示条数", required = true, dataType = "Integer", example = "10")
    })
    public TableData hightSearch(CarSourceSearchPojo pojo, int page, int limit) {

        //分页设置
        pojo.setStart((page - 1) * limit);
        pojo.setRows(limit);

        String json = JSONUtil.toJson(pojo);

        //远程调用---统计es数据的数量
        long count = carSourceSearchServiceClient.searchCount(json);
        if (count > 0) {
            //远程调用---进行es日志查询
            List<Map> list = carSourceSearchServiceClient.hightSearch(json);

            //TODO:是否可以考虑,把从es中查询到的日志数据,缓存到redis中?---->key=logs_1/2/3,value=list

            //分页数量需要单独设置
            return new TableData<>(count, list);
        }

        return new TableData<>(0L, null);
    }

}
